﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PeapodCatcher
{
    /// <summary>
    /// Interaction logic for FeedDetail.xaml
    /// </summary>
    public partial class FeedDetail : Window
    {
        private Feed _feed;
        public FeedDetail(Feed feed)
        {
            InitializeComponent();

            _feed = feed;
            ObjectDataProvider feedProvider = (ObjectDataProvider)FindResource("feed");
            feedProvider.ObjectInstance = _feed;
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public delegate void FeedUpdated(Object sender, EventArgs e);
        public event FeedUpdated OnFeedUpdated;

        private void OK_Click(object sender, RoutedEventArgs e)
        {

            title.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            url.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            keepAtMost.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            removeAfter.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            push.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            parseMediaFly.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            useSubPath.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            if (useSubPath.IsChecked.GetValueOrDefault())
            {
                _feed.SubPathName = Common.SafeFileName(title.Text);
            }

            using (Data db = new Data(Common.DatabasePath))
            {
                db.Feed.Attach(_feed);
                db.SubmitChanges();
            }
            
            if (OnFeedUpdated != null)
            {
                OnFeedUpdated(this, new EventArgs());
            }

            this.Close();
        }


    }
}
